<?php

namespace App\Providers;

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        !defined('REQUEST_MOBILE_FAILED') && define('REQUEST_MOBILE_FAILED', '请求失败，请检查设备是否在线');

        error_reporting(E_ALL ^ E_NOTICE);
        Schema::defaultStringLength(191);
        DB::enableQueryLog();
//        Horizon::auth(function () {
//            return ($this->app->environment() !== 'production' || !Auth::guard('admin')->guest()) ? true : false;
//        });

        if ($this->app->environment() === 'local') {
            DB::listen(function ($query) {
                $sql = str_replace(["?", "%Y-%m-%d", "%Y-%m"], ["'%s'", "?Y-?m-?d", "?Y-?m"], $query->sql);
                $sql = vsprintf($sql, $query->bindings);
                $sql = str_replace(["\\", "?Y-?m-?d", "?Y-?m"], ["", "%Y-%m-%d", "%Y-%m"], $sql);
                Log::info($sql);
            });
        }

        //超时任务
//        Queue::after(function (JobProcessed $event) {
//            if ($event->job->timeoutAt()) {
//                DingDing::push($event->job->payload(), true);
//            }
//        });
    }

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        if ($this->app->environment() === 'local') {
            /*
            * Ide Helper
            */
            $this->app->register('Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider');
        }

        //批量注册
        foreach (glob(base_path('app/Repositories/') . '*.php') as $filename) {
            $class = trim(basename($filename), '.php');

            $this->app->singleton($class, function () use ($class) {
                return app('App\Repositories\\' . $class);
            });
        }
    }

}
